﻿using System;
using System.Collections;
using System.Web.UI.WebControls;
using Inkubator.Wizard.Model.Model;

namespace Inkubator.Wizard.WizardGenerator.Validator
{
    public class WizardValidator
    {
        private Hashtable hashValidators;

        public WizardValidator()
        {
            hashValidators = new Hashtable();
            hashValidators.Add("required", WizardValidatorFactory.ValidatorType.Required);
            hashValidators.Add("range", WizardValidatorFactory.ValidatorType.Range);
        }

        /// <summary>
        /// This method add the Validation component to the respective WebControl.
        /// </summary>
        /// <param name="component"></param>
        /// <param name="control"></param>
        /// <returns></returns>
        public Panel applyValidation( WizardComponent component, WebControl control)
        {
            Panel panel = new Panel();
            control.ID = component.ID;

            try
            {
                panel.Controls.Add(control);

                //Split the validate value to work with all the options


                BaseValidator validator = WizardValidatorFactory.validatorFactory(
                    ((WizardValidatorFactory.ValidatorType)hashValidators[component.validate]));

                validator.ErrorMessage = "* Error";
                validator.ControlToValidate = control.ID.ToString();
                validator.ID = "Val" + control.ID.ToString();

                panel.Controls.Add(validator);
            }
            catch (Exception exc)
            {
                Console.WriteLine(exc.ToString());
                panel.Controls.Add(control);
            }

            return panel;
        }

    }
}
